const express = require('express')
//数据库引入
const adminModel = require("../db/model/adminModel")
const md5 = require('md5');
const route = express.Router()

const addAdminReg = require("../middleware/addAdminReg")
const isLoginMiddle = require("../middleware/isLoginMiddle")

route.post("/addAdmin",addAdminReg,isLoginMiddle,async (req, res) => {
  //拿到请求体数据
  const {
    adminName,
    passWord,
    regTime
  } = req.body;
  // console.log(adminName, passWord, regTime);
  // console.log(adminModel)
  // res.send('ok')
  //拿着用户名去数据库中查询是否存在。
  try {
    const findRe = await adminModel.findOne({
      adminName
    })
    if(findRe){
      return res.send({
        code: 10001,
        msg: "添加管理员-用户已经被注册",
        data: null
      })
    }
    await adminModel.create({
      adminName,
      passWord:md5(passWord+'8423') ,
      regTime
    })
    res.send({
      code: 10000,
      msg: "添加管理员-添加成功",
      data: null
    })

  }catch (e){
    console.log("出现错误"+e.message)
    res.send({
      code: 10002,
      msg: "添加管理员-服务端异常",
      data: null
    })
  }
})
module.exports = route